﻿using System;
using System.Windows.Forms;
using DataBaseLayer.Abstractions;
using DataBaseLayer.Abstractions.Interfaces;
using DocumentManager.Abstractions.Factories;
using DocumentManager.Abstractions.Interfaces;
using DocumentManager.Enums;
using DocumentManager.Extensions;

namespace DocumentManager.Forms
{
	public partial class frmDocumentManager : Form
	{
		#region Variables

		private readonly FormBaseFactory _formFactory;
		private readonly ServiceBaseFactory _serviceFactory;

		#endregion

		#region Properties

		private OrderType OrderType { get; set; }

		#endregion

		#region Constructors

		public frmDocumentManager(
			FormBaseFactory formFactory,
			ServiceBaseFactory serviceFactory)
		{
			if (formFactory == null)
				throw new ArgumentNullException(string.Format("form Factory"));
			_formFactory = formFactory;

			if (serviceFactory == null)
				throw new ArgumentNullException(string.Format("service Factory"));
			_serviceFactory = serviceFactory;

			InitializeComponent();
		}

		#endregion

		#region Form Actions

		private void tsmiBusinessTripOrder_Click(object sender, EventArgs e)
		{
			OrderType = OrderType.BusinessTrip;
			InicializeMainPanel();
		}

		private void tsmiDismissalOrder_Click(object sender, EventArgs e)
		{
			OrderType = OrderType.Dismissal;
			InicializeMainPanel();
		}

		private void tsmiLeaveOrder_Click(object sender, EventArgs e)
		{
			OrderType = OrderType.Leave;
			InicializeMainPanel();
		}

		private void tsmiLogOrdersJournal_Click(object sender, EventArgs e)
		{
			this.Enabled = false;

			var form = _formFactory.CreateForm(FormType.LogOrders);
			if (form != null)
				form.ShowDialog();

			this.Enabled = true;
		}

		private void tsmiInfoCompanySettings_Click(object sender, EventArgs e)
		{
			this.Enabled = false;

			var form = _formFactory.CreateForm(FormType.InfoCompanySettings);
			if (form != null)
				form.ShowDialog();

			this.Enabled = true;
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			this.Enabled = false;

			var form = _formFactory.CreateOrderForm(OrderType);
			if (form != null)
			{
				form.ShowDialog();
				InicializeMainPanel();
			}

			this.Enabled = true;
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			this.Enabled = false;

			var selectedItem = cbData.SelectedItem as IOrder;
			if (selectedItem == null)
			{
				MessageBox.Show(string.Format("Выберите приказ для редактирования!"));
				this.Enabled = true;
				return;
			}

			var form = _formFactory.CreateOrderForm(OrderType);
			if (form is IOrderForm)
			{
				(form as IOrderForm).OrderId = selectedItem.Id;
				form.ShowDialog();
				InicializeMainPanel();
			}

			this.Enabled = true;
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			this.Enabled = false;

			var selectedItem = cbData.SelectedItem as IOrder;
			if (selectedItem == null)
			{
				MessageBox.Show(string.Format("Выберите приказ для удаления!"));
				this.Enabled = true;
				return;
			}

			switch (OrderType)
			{
				case OrderType.Leave:
					var leaveService = _serviceFactory.CreateLeaveOrdersService();
					if (leaveService != null)
						leaveService.DeleteOrderById(selectedItem.Id);
					break;

				case OrderType.Dismissal:
					var dismissalService = _serviceFactory.CreateDismissalOrdersService();
					if (dismissalService != null)
						dismissalService.DeleteOrderById(selectedItem.Id);
					break;

				case OrderType.BusinessTrip:
					var businessTripService = _serviceFactory.CreateBusinessTripOrdersService();
					if (businessTripService != null)
						businessTripService.DeleteOrderById(selectedItem.Id);
					break;

				default:
					throw new NotImplementedException(string.Format("not implemented this order type"));
			}

			InicializeMainPanel();

			this.Enabled = true;
		}

		#endregion

		#region Internal Implementation

		private void InicializeMainPanel()
		{
			pMain.Visible = true;

			object comboBoxDataSource = null;
			switch (OrderType)
			{
				case OrderType.Leave:
					var leaveService = _serviceFactory.CreateLeaveOrdersService();
					if (leaveService != null)
						comboBoxDataSource = leaveService.GetOrders();
					break;

				case OrderType.Dismissal:
					var dismissalService = _serviceFactory.CreateDismissalOrdersService();
					if (dismissalService != null)
						comboBoxDataSource = dismissalService.GetOrders();
					break;

				case OrderType.BusinessTrip:
					var businessTripService = _serviceFactory.CreateBusinessTripOrdersService();
					if (businessTripService != null)
						comboBoxDataSource = businessTripService.GetOrders();
					break;

				default:
					throw new NotImplementedException(string.Format("not implemented this order type"));
			}

			cbData.Clear();
			cbData.DataSource = comboBoxDataSource;
			cbData.DisplayMember = "Number";

			lblActionName.Text = OrderType.GetOrderTypeDescription();
		}

		#endregion
	}
}
